package io.renren.controller;

import io.renren.entity.SysGroupEntity;
import io.renren.service.SysGroupService;
import io.renren.utils.PageUtils;
import io.renren.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2017-03-16 16:31:33
 */
@Controller
@RequestMapping("sysgroup")
public class SysGroupController {
    @Autowired
    private SysGroupService sysGroupService;

    @RequestMapping("/sysgroup.html")
    public String list() {
        return "sysgroup/sysgroup.html";
    }

    /**
     * 列表
     */
    @ResponseBody
    @RequestMapping("/list")
    @RequiresPermissions("sysgroup:list")
    public R list(Integer page, Integer limit) {
        Map<String, Object> map = new HashMap<>();
        map.put("offset", (page - 1) * limit);
        map.put("limit", limit);

        //查询列表数据
        List<SysGroupEntity> sysGroupList = sysGroupService.queryList(map);
        int total = sysGroupService.queryTotal(map);

        PageUtils pageUtil = new PageUtils(sysGroupList, total, limit, page);

        return R.ok().put("page", pageUtil);
    }


    /**
     * 信息
     */
    @ResponseBody
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sysgroup:info")
    public R info(@PathVariable("id") Integer id) {
            SysGroupEntity sysGroup = sysGroupService.queryObject(id);

        return R.ok().put("sysGroup", sysGroup);
    }

    /**
     * 保存
     */
    @ResponseBody
    @RequestMapping("/save")
    @RequiresPermissions("sysgroup:save")
    public R save(@RequestBody SysGroupEntity sysGroup) {
        sysGroup.setCreateTime(new Date());
            sysGroupService.save(sysGroup);
        return R.ok();
    }

    @ResponseBody
    @RequestMapping(value = "/ajaxSave")
    public R ajaxSave(@RequestBody SysGroupEntity sysGroup, HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin","*");
        sysGroup.setCreateTime(new Date());
        sysGroupService.save(sysGroup);
        return R.ok();
    }


    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("sysgroup:update")
    public R update(@RequestBody SysGroupEntity sysGroup) {
            sysGroupService.update(sysGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @ResponseBody
    @RequestMapping("/delete")
    @RequiresPermissions("sysgroup:delete")
    public R delete(@RequestBody Integer[]ids) {
            sysGroupService.deleteBatch(ids);

        return R.ok();
    }

}
